import ImageModel from "../../models/Image";
import PageModel from "../../models/Page";
import Crypt from "../../utils/Crypt";
import Avatar from "../../utils/Avatar";
import UserModel from "../../models/User";
import config from "../../config/config";

const imageModel = new ImageModel()
const pageModel = new PageModel()
const userModel = new UserModel()

Page({
  data: {
    image: '',
    swiper1: [],
    swiper2: [],
    user: {},
    isLogin: false,
    is_area: false,
    c1: '',
    c2: '',
    wx: false
  },

  // onTabItemTap(item) {
  //   // console.log(item)
  //   pageModel.getWx().then(response => {
  //     const { data } = response
  //     wx.setStorageSync('wx-set', data.wx)
  //   })
  // },

  onLoad: function (options) {

    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success: () => {
        // console.log('ok')
        // 可以把分享记录保存到数据库
      }
    })



  },

  // 先给提示
  onShow() {

    // 审核
    pageModel.getWx().then(response => {
      const { data } = response
      wx.setStorageSync('wx-set', data.wx)
      this.setData({ wx: data.wx })
    })


    userModel.checkLogin(true).then(() => {

      // user info
      this.getUserInfo()

      // bg image
      imageModel.getRemoteImage().then(data => {
        const image = data.bg.center
        const c1 = config.app_base_url + data.bg.c1
        const c2 = config.app_base_url + data.bg.c2
        // console.log(data.bg)
        this.setData({
          image,
          c1,
          c2
        })
      })

      // uni , swiper
      // pageModel.getCenter().then(response => {
      //   let { data } = response
      //   data = Crypt.parse(data)
      //   this.setData({ ...data })
      // })
    })
  },

  getUserInfo() {

    // get user info
    userModel.getInfo().then(response => {
      let { data } = response
      data = Crypt.parse(data)
      if (data.address_id > 0) {
        wx.setStorageSync('Address_id', data.address_id)
      }

      if (!data.mobile) {
        wx.redirectTo({
          url: '/sub_main/pages/user/mobile/index'
        })
      } else if (!data.avatar || !data.nickname) {
        // 这是老的, 只能获取灰色头像,
        // 现在用新的
        // wx.redirectTo({
        //   url: '/sub_main/pages/user/get-info/index'
        // })

        // 不能太强制了, 审核可能不会通过
        const key = 'update_avatar'
        const is = wx.getStorageSync(key)
        if (!is) {
          wx.setStorageSync(key, true)
          wx.redirectTo({
            url: '/sub_main/pages/user/avatar/index'
          })
        } else {
          this.setData({ user: data, isLogin: true })
        }

      } else {
        // if (data.base64Image && data.base64Image.length > 0) {
        //   data.avatar = data.base64Image
        // }
        //
        // this.setData({ user: data })
        data.avatar = Avatar(data)
        // console.log(data.avatar)

        // 是否指定区代
        this.setData({ user: data, isLogin: true, is_area: data.address_up == 0 })


      }
    })

  },

  // 申请区代
  regArea() {
    userModel.regArea().then(() => {
      wx.showToast({
        title: '申请成功,等待审核中...',
        icon: 'none'
      })
    })
  },

  goService() {
    wx.navigateTo({
      url: '/sub_common/pages/service/index'
    })
  },

  goWx() {
    wx.navigateTo({
      url: '/sub_service/pages/wx/index'
    })
  }

});
